<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="提交人ID" prop="submitterId">
        <el-input
          v-model="queryParams.submitterId"
          placeholder="请输入提交人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="回复人ID" prop="responderId">
        <el-input
          v-model="queryParams.responderId"
          placeholder="请输入回复人ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校ID" prop="schoolId">
        <el-input
          v-model="queryParams.schoolId"
          placeholder="请输入学校ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系方式" prop="contactInfo">
        <el-input v-model="queryParams.contactInfo" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label-width="120px" label="提交日期和时间" prop="dateTime">
        <el-date-picker clearable v-model="queryParams.dateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择提交日期和时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label-width="120px" label="回复日期和时间" prop="responseDateTime">
        <el-date-picker clearable v-model="queryParams.responseDateTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择回复日期和时间">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="是否已解决" prop="resolved">
        <el-input
          v-model="queryParams.resolved"
          placeholder="请输入是否已解决"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-view" size="mini" @click="getList(false)"
          v-hasPermi="['business:suggestions:add']">查看所有记录</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['com/ruoyi/business:suggestions:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['com/ruoyi/business:suggestions:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['com/ruoyi/business:suggestions:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border stripe :header-cell-style="rowClass" v-loading="loading" :data="suggestionsList"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column :resizable="false" label="序号" align="center" type="index" width="100" />
      <!-- <el-table-column label="主键ID" align="center" prop="suggestionsId" />
      <el-table-column label="提交人ID" align="center" prop="submitterId" />
      <el-table-column label="回复人ID" align="center" prop="responderId" />
      <el-table-column label="学校ID" align="center" prop="schoolId" /> -->
      <el-table-column :resizable="false" label="回复人姓名" align="center" prop="responderName" />
      <el-table-column :resizable="false" label="姓名" align="center" prop="name" />
      <el-table-column :resizable="false" label="联系方式" align="center" prop="contactInfo" />
      <el-table-column :resizable="false" label="提交日期和时间" align="center" prop="dateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="投诉或建议的内容" align="center" prop="description"/> -->
      <el-table-column :resizable="false" label="期望的解决方案" align="center" prop="expectedResolution" />
      <!-- <el-table-column label="回复内容" align="center" prop="response" /> -->
      <el-table-column :resizable="false" label="回复日期和时间" align="center" prop="responseDateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.responseDateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="是否已解决" align="center" prop="resolved">
        <template slot-scope="scope">
          <el-tag effect="dark" :type="scope.row.resolved == 0 ? 'danger' : 'success'">
            {{ scope.row.resolved == 0 ? "未解决" : "已解决" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column :resizable="false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="!scope.row.resolved" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)" v-hasPermi="['business:suggestions:edit']">回复</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="viewDetails(scope.row)"
            v-hasPermi="['business:suggestions:remove']">查看投诉或回复详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 查看详情 -->
    <el-dialog :title="title" :visible.sync="detailsOpen" width="50%" append-to-body>
      <el-descriptions :column="1" :border="true" :labelStyle="{width:'100px',textAlign: 'center',color:'#000' }">
        <el-descriptions-item label="投诉建议">{{form.description}}</el-descriptions-item>
        <el-descriptions-item label="回复内容"><div>{{form.response==""?"还没有回复":form.response}}</div></el-descriptions-item>
      </el-descriptions>
    </el-dialog>
    <!-- 添加或修改用于记录投诉和建议的信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <!-- <el-form-item label="提交人ID" prop="submitterId">
          <el-input v-model="form.submitterId" placeholder="请输入提交人ID" />
        </el-form-item>
        <el-form-item label="回复人ID" prop="responderId">
          <el-input v-model="form.responderId" placeholder="请输入回复人ID" />
        </el-form-item>
        <el-form-item label="学校ID" prop="schoolId">
          <el-input v-model="form.schoolId" placeholder="请输入学校ID" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contactInfo">
          <el-input v-model="form.contactInfo" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="提交日期和时间" prop="dateTime">
          <el-date-picker clearable
            v-model="form.dateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择提交日期和时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="投诉或建议的详细内容" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <el-form-item label="期望的解决方案" prop="expectedResolution">
          <el-input v-model="form.expectedResolution" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="回复内容" prop="response">
          <el-input v-model="form.response" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否已解决" prop="resolved">
          <el-radio-group v-model="form.resolved">
            <el-radio :label="1">已解决</el-radio>
            <el-radio :label="0">未解决</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="回复日期和时间" prop="responseDateTime">
          <el-date-picker clearable
            v-model="form.responseDateTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择回复日期和时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否已解决" prop="resolved">
          <el-input v-model="form.resolved" placeholder="请输入是否已解决" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSuggestions, getSuggestions, delSuggestions, addSuggestions, updateSuggestions } from "@/api/complaintAddSuggestions/complaintAddSuggestions";

export default {
  name: "Suggestions",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用于记录投诉和建议的信息表格数据
      suggestionsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //查看详情
      detailsOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inform: true,
        submitterId: null,
        responderId: null,
        schoolId: null,
        name: null,
        contactInfo: null,
        dateTime: null,
        description: null,
        expectedResolution: null,
        response: null,
        responseDateTime: null,
        resolved: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        submitterId: [
          { required: true, message: "提交人ID不能为空", trigger: "blur" }
        ],
        schoolId: [
          { required: true, message: "学校ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        contactInfo: [
          { required: true, message: "联系方式不能为空", trigger: "blur" }
        ],
        dateTime: [
          { required: true, message: "提交日期和时间不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "投诉或建议的详细内容不能为空", trigger: "blur" }
        ],
        expectedResolution: [
          { required: true, message: "期望的解决方案不能为空", trigger: "blur" }
        ],
        response: [
          { required: true, message: "回复内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    //设置table标题样式
    rowClass ({ row, rowIndex }) {
      return 'font-size: 15px; background-color: #FAFAFA;'
    },
    // 查看详情
    viewDetails (row) {
      this.reset();
      const suggestionsId = row.suggestionsId || this.ids
      getSuggestions(suggestionsId).then(response => {
        this.form = response.data;
        this.detailsOpen = true;
        this.title = "投诉建议和回复详情";
      });
    },
    /** 查询用于记录投诉和建议的信息列表 */
    getList (inform) {
      if (!inform) {
        this.queryParams.inform = inform
      }
      this.loading = true;
      listSuggestions(this.queryParams).then(response => {
        this.suggestionsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        suggestionsId: null,
        submitterId: null,
        responderId: null,
        schoolId: null,
        name: null,
        contactInfo: null,
        dateTime: null,
        description: null,
        expectedResolution: null,
        response: null,
        responseDateTime: null,
        resolved: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.suggestionsId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加用于记录投诉和建议的信息";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const suggestionsId = row.suggestionsId || this.ids
      getSuggestions(suggestionsId).then(response => {
        this.form = response.data;
        this.open = true;
        // this.title = "修改用于记录投诉和建议的信息";
        this.title = "回复";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.suggestionsId != null) {
            updateSuggestions(this.form).then(response => {
              this.$modal.msgSuccess("回复成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSuggestions(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const suggestionsIds = row.suggestionsId || this.ids;
      this.$modal.confirm('是否确认删除用于记录投诉和建议的信息编号为"' + suggestionsIds + '"的数据项？').then(function () {
        return delSuggestions(suggestionsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('business/suggestions/export', {
        ...this.queryParams
      }, `suggestions_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
